﻿using CandidateManager.Areas.Coach.Models;
using System.Collections.Generic;
using System.Web.Mvc;
using WI.CandidateManager.Business.Objects;
using WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Areas.Coach.Controllers
{
    /// <summary>
    /// Controller for working with Homeworks.
    /// </summary>
    public class HomeworksController : CandidateManager.Controllers.AutorizeBaseController
    {
        #region Homework actions

        /// <summary>
        /// Display table with list of own Homeworks.
        /// </summary>
        /// <returns>Table with list of own Homeworks</returns>
        [Authorize]
        public ActionResult List()
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetHomeworkList());
        }

        /// <summary>
        /// Display table with list of All Homeworks.
        /// </summary>
        /// <returns>Table with list of All Homeworks</returns>
        [Authorize]
        public ActionResult AllList()
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetAllHomeworkList());
        }

        /// <summary>
        /// Display table with list of Pending Homeworks.
        /// </summary>
        /// <returns>Table with list of Pending Homeworks</returns>
        [Authorize]
        public ActionResult PendingList()
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetPendingTestList());
        }

        /// <summary>
        /// Get object of Homework for displaying details.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <returns>View of Homework.</returns>
        [Authorize]
        public ActionResult Detail(long HomeworkId)
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetHomework(HomeworkId));
        }

        /// <summary>
        /// Get object of Homework for checking.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <returns>View of Homework.</returns>
        [Authorize]
        public ActionResult Check(long HomeworkId)
        {
            return View();
        }

        /// <summary>
        /// Display page for creating new Homework.
        /// </summary>
        /// <returns>Page for creating new Homework.</returns>
        [HttpGet]
        [Authorize]
        public ActionResult Create()
        {
            Homework homework = new Homework();

            return View(homework);
        }

        /// <summary>
        /// Send object of Homework to logic for creating.
        /// </summary>
        /// <param name="homework">Object of Homework.</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult Create(Homework homework)
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            if (homeworkBO.CreateNewHomework(homework))
            {
                return RedirectToAction("List", "Homeworks");
            }
            else
            {
                return View(homework);
            }
        }

        /// <summary>
        /// Get model of Homework for editing.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <returns>View of Homework for editing.</returns>
        [HttpGet]
        [Authorize]
        public ActionResult Edit(long HomeworkId)
        {
            HomeworkBO homeworkBO = new HomeworkBO();
            var homework = homeworkBO.GetHomework(HomeworkId);

            return View(homework);
        }

        /// <summary>
        /// Send object of Homework to logic for editing.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <param name="homework">Object of Homework.</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult Edit(long HomeworkId, Homework homework)
        {
            HomeworkBO homeworkBO = new HomeworkBO();
            var curHomework = homeworkBO.GetHomework(HomeworkId);

            if (homeworkBO.ChangeHomework(HomeworkId, homework))
            {
                return RedirectToAction("List", "Homeworks");
            }

            return View(curHomework);
        }

        /// <summary>
        /// Get model of Homework for deleting.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult Delete(long HomeworkId)
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetHomework(HomeworkId));
        }

        /// <summary>
        /// Send object of Homework to logic for deleting.
        /// </summary>
        /// <param name="HomeworkId">Homework id.</param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete")]
        [Authorize]
        public ActionResult DeleteConfirmed(long HomeworkId)
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            if (homeworkBO.DeleteHomework(HomeworkId))
            {
                return RedirectToAction("List", "Homeworks");
            }

            return View(HomeworkId);
        }

        #endregion

        #region Assign actions


        [Authorize]
        public ActionResult Assign(long HomeworkId)
        {
            HomeworkBO homeworkBO = new HomeworkBO();

            return View(homeworkBO.GetHomework(HomeworkId));
        }


        [Authorize]
        public ActionResult RemoveCandidate(long CandidateId, long HomeworkId)
        {
            CoachBO coachBO = new CoachBO();

            coachBO.DeassingTaskToCandidate(HomeworkId, CandidateId);

            return RedirectToAction("Assign", "Homeworks", new { HomeworkId = HomeworkId });
        }


        [Authorize]
        [HttpGet]
        public ActionResult AssignToGroups(long HomeworkId)
        {
            CoachBO coachBO = new CoachBO();
            HomeworkBO homeworkBO = new HomeworkBO();

            var groups = coachBO.GetGroups();
            GroupAssignModel groupAssignModel = new GroupAssignModel();
            groupAssignModel.Task = homeworkBO.GetHomework(HomeworkId);
            groupAssignModel.GroupCheckList = new List<GroupChecklistModel>();

            groupAssignModel.Task = homeworkBO.GetHomework(HomeworkId);
            foreach (var group in groups)
            {
                groupAssignModel.GroupCheckList.Add(new GroupChecklistModel() { IsChecked = false, Group = group, GroupId = group.Id });
            }

            return View(groupAssignModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AssignToGroups(long TaskId, GroupAssignModel groupAssignModel)
        {
            CoachBO coachBO = new CoachBO();

            foreach (var group in groupAssignModel.GroupCheckList)
            {
                if (group.IsChecked)
                {
                    coachBO.AssignTaskToGroup(TaskId, group.GroupId);
                }
            }

            return RedirectToAction("Assign", "Homeworks", new { HomeworkId = TaskId });
        }

        [Authorize]
        [HttpGet]
        public ActionResult AssignToCandidates(long HomeworkId)
        {
            CoachBO coachBO = new CoachBO();
            HomeworkBO homeworkBO = new HomeworkBO();

            var candidates = coachBO.GetCoachesCandidates();
            CandidateAssignModel candidateAssignModel = new CandidateAssignModel();
            candidateAssignModel.Task = homeworkBO.GetHomework(HomeworkId);
            candidateAssignModel.CandidateCheckList = new List<CandidateChecklistModel>();

            foreach (var candidate in candidates)
            {
                candidateAssignModel.CandidateCheckList.Add(new CandidateChecklistModel() { CandidateId = candidate.Id, IsChecked = false, Candidate = candidate });
            }

            return View(candidateAssignModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AssignToCandidates(long TaskId, CandidateAssignModel candidateAssignModel)
        {
            CoachBO coachBO = new CoachBO();

            foreach (var candidate in candidateAssignModel.CandidateCheckList)
            {
                if (candidate.IsChecked)
                {
                    coachBO.AssignTaskToCandidate(TaskId, candidate.CandidateId);
                }
            }

            return RedirectToAction("Assign", "Homeworks", new { HomeworkId = TaskId });
        }

        #endregion
    }
}
